<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
	<base href="<%=basePath%>">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
    <title>注册</title>
    <link rel="stylesheet" href="css/style.css" />
<body>

<div class="register-container">
	<h1>注册</h1>

	<div class="connect">
		<p>请注册您的信息</p>
	</div>
	
	<div id="registerForm">
		<div>
			<input id="userName" type="text" name="userName" class="username" placeholder="您的用户名"/>
		</div>
		<div>
			<input id="password" type="password" name="password" class="password" placeholder="输入密码" />
		</div>
		<div>
			<input id="realName" type="text" name="realName" class="confirm_password" placeholder="输入真实姓名" />
		</div>
		<div>
			<input id="age" type="" name="age"  placeholder="输入年龄" autocomplete="off" />
		</div>
		<div>
			<input id="tel" type="text" name="tel" class="phone_number" placeholder="输入手机号" autocomplete="off"/>
		</div>
		<div>
			<input id="email" type="email" name="email" class="email" placeholder="输入邮箱地址"  />
		</div>
		<div>
			<input id="idCard" type="text" name="idCard"  placeholder="输入身份证号" />
		</div>
		<button id="submit" type="button">注 册</button>
	</div>


	<a href="index.jsp">
		<button type="button" class="register-tis">已经有账号？</button>
	</a>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/common.js"></script>
<script src="js/supersized.3.2.7.min.js"></script>
<script src="js/supersized-init.js"></script>
<script src="js/jquery.validate.min.js?var1.14.0"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#userName").click(function () {
            var userName = $("input[name='userName']").val();
            $.ajax({
                url: '/user/findUsername',
                type: 'POST',
                dataType: 'json',
                data: "userName=" + userName,
                success: function (data) {
                    if (data != null) {
                        alert("用户名已存在")
                        return false;
                    } else {
                        alert("用户名可以使用")
                        return false;
                    }
                }, error: function () {
                    alert("异常")
                }
            })
        });

        $("#submit").click(function () {
            var userName = $("input[name='userName']").val();
            var password = $("input[name='password']").val();
            var realName = $("input[name='realName']").val();
            var age = $("input[name='age']").val();
            var tel = $("input[name='tel']").val();
            var email = $("input[name='email']").val();
            var idCard = $("input[name='idCard']").val();

            if ($("[name=userName]").val() == "") {
                alert("用户名不能为空");
                return false;
            }
            var a = /^[0-9a-zA-Z]*$/g;
            if (a.test($("[name=userName]").val()) == false) {
                alert("用户名只能由英文数字组成");
                return false;
            }
            if ($("[name=password]").val() == "") {
                alert("密码不能为空");
                return false;
            }
            if ($("[name=realName]").val() == "") {
                alert("真实姓名不能为空");
                return false;
            }
            if ($("[name=age]").val() == "") {
                alert("年龄不能为空");
                return false;
            }
            if ($("[name=tel]").val() == "") {
                alert("手机号不能为空");
                return false;
            }
            var c = /^[1][3,4,5,7,8][0-9]{9}$/;
            if (c.test($("[name=tel]").val()) == false) {
                alert("您输入的手机号格式不正确");
                return false;
            }
            if ($("[name=email]").val() == "") {
                alert("邮箱不能为空");
                return false;
            }
            var b = /^\d{5,12}@[qq][qq]\.com$/;
            if (b.test($("[name=email]").val()) == false) {
                alert("您输入的邮箱格式不正确");
                return false;
            }
            if ($("[name=idCard]").val() == "") {
                alert("身份证号不能为空");
                return false;
            }
      /*      var d = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

            if ($(d.test("[name=idCard]").val()) == false) {
                alert("您输入的身份证号不合法");
                return false;
            }*/
            $.ajax({
                url: '/user/add',
                type: 'POST',
                dataType: 'json',
                data: "userName=" + userName + "&password=" + password + "&realName=" + realName + "&age="+ age + "&tel=" + tel + "&email=" + email + "&idCard=" + idCard,
                success: function (data) {
                    if (data==0) {
                        alert("注册失败");
                    } else {
                        alert("注册成功");
                        window.location = "index.jsp";
                    }
                },
            });
        })

    });
</script>
</body>
</html>
